﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.BLL;
using OgilvyOne.Model;

public partial class admin_infomation_info_list : AdminBasePage
{
    public int isaudit = 0;
    public static readonly string ArticleVideoLayerids = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleVideoLayerids");
    public static readonly string OrderByArticleLayerids = OgilvyOne.Utility.ConfigUtil.GetStringValue("OrderByArticleLayerids"); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (isExist(Layerid, OrderByArticleLayerids))
            {
                this.ordername.Items.Add(new ListItem("排序值", "order_num"));
                /*this.ordername.Text = "order_num";
                this.ordertype.Text = "asc"; */
            }

        }

        //if (!IsPostBack)
        {
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);
            Grid_viewBild();
        }

        if (this.ordername.Text == "order_num" && this.ordertype.Text == "asc")
        {
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                this.GridView1.Rows[i].FindControl("order").Visible = true;
                this.GridView1.Rows[i].FindControl("ordernext").Visible = true;
            }
        }
        
    }

    private void Grid_viewBild()
    {

        
        int columnid = int.Parse(Request.Params["columnid"]);
        ColumnModel cm = new ColumnBll().GetColumn(columnid);
        isaudit = cm.Isauditing;

        this.audittd.Visible = false;
        this.pubtd.Visible = false;
        CmsArticleQuery cq = new CmsArticleQuery();
        cq.columnid = columnid;
        CmsArticleBll cabll = new CmsArticleBll();
        int pagesize = 10;

        if (Request.Params["qdocstate"] != null && Request.Params["qdocstate"] != "")
        {
            cq.docstate = Request.Params["qdocstate"];
            if (cq.docstate == "0")
            {


                if (isaudit == 1)
                {
                    this.audittd.Visible = true;
                }
                else
                {
                    this.pubtd.Visible = true;
                }

            }
            else if (cq.docstate == "2")
            {
                this.pubtd.Visible = true;
            }
            
            
        }

        if(Request.Params["rows"]!=null&&Request.Params["rows"]!="")
        {
            pagesize = int.Parse(Request.Params["rows"]);
            this.rows.Text = pagesize.ToString();
        }
        if (Request.Params["ordername"] != null && Request.Params["ordername"] != "")
        {
            cq.ordername = Request.Params["ordername"];
            
        }
        cq.ordername = this.ordername.Text;
        if(Request.Params["ordertype"] != null && Request.Params["ordertype"]!="")
        {
            cq.ordertype = Request.Params["ordertype"];
            
        }
        cq.ordertype = this.ordertype.Text ;
        if (Request.Params["querykey"] != null && Request.Params["querykey"] != "")
        {
            cq.querykey = Request.Params["querykey"];
            this.querykey.Text = cq.querykey;
        }
        if (Request.Params["queryvalue"] != null && Request.Params["queryvalue"] != "")
        {
            cq.queryvalue = Request.Params["queryvalue"];
            this.queryvalue.Text = cq.queryvalue;
        }
        if (Request.Params["querytype"] != null && Request.Params["querytype"] != "")
        {
            cq.querytype = Request.Params["querytype"];
            this.querytype.Text = cq.querytype;
        }

        if (Request.Params["pageNum"] != null && Request.Params["pageNum"] != "")
        {
            try
            {
                GridView1.PageIndex = int.Parse(Request.Params["pageNum"]) - 1;
            }
            catch { }
        }
        IList datalist = cabll.getCmsArticleList(cq);

        if (datalist.Count < 1)
        {
            this.deltd.Visible = false;
            this.pubtd.Visible = false;
            this.audittd.Visible = false;
        }

        GridView1.DataSource = datalist;
        GridView1.PageSize = pagesize;
        GridView1.DataBind();
        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, datalist.Count);
    }

    protected void Add_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], ADD);
        Response.Redirect("info_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Del_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], DEL);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().DelCmsArticle(ids);
        if (r)
        {
            this.insertLog("批量删除文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Delone_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], DEL);
        string aid = ((LinkButton)sender).CommandArgument;
        
        bool r = new CmsArticleBll().DelCmsArticle(aid);
        if (r)
        {
            insertLog("删除文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }

    protected void Pub_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], PUB);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().Pubs(ids, GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("批量发布文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Pubone_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], PUB);
        
        string aid = ((LinkButton)sender).CommandArgument;
        bool r = new CmsArticleBll().Pubs(aid, GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("发布文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }
    //撤消发布
    protected void RePubone_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], PUB);

        string aid = ((LinkButton)sender).CommandArgument;
        bool r = new CmsArticleBll().RePubOne(int.Parse(aid), GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("撤消发布文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }

    protected void Mod_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], MOD);
        Response.Redirect("info_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&id=" + ((LinkButton)sender).CommandArgument, true);
    }
    protected void Seo_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], MOD);
        Response.Redirect("info_seo.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&id=" + ((LinkButton)sender).CommandArgument, true);
    }
    protected void Audit_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], AUDIT);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().Audits(ids,"2", GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("批量审核文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Preview_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], QUERY);
        ClientScript.RegisterStartupScript(this.GetType(), "infopreview", "<script type='text/javascript'>window.open('info_preview.aspx?cid=" + Request.Params["columnid"] + "&aid=" + ((LinkButton)sender).CommandArgument + "','_blank');</script>");
        
    }




    protected void orderBtn_click(object sender, EventArgs e)
    {
        PopedomValidate(Layerid, MOD);
        int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
        if (row > 0)
        {
            int aid = int.Parse(((LinkButton)sender).CommandArgument);
            int ordernum = new CmsArticleBll().ModOrderNum(aid, 0);
            Page_Load(sender, e);
        }
    }

    protected void ordernextBtn_click(object sender, EventArgs e)
    {
        PopedomValidate(Layerid, MOD);

        int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
        if (row < this.GridView1.Rows.Count -1 )
        {
            int aid = int.Parse(((LinkButton)sender).CommandArgument);
            int ordernum = new CmsArticleBll().ModOrderNum(aid, 1);
            Page_Load(sender, e);
        }

    }
   
}
